from mrjob.job import MRJob
from mrjob.protocol import TextProtocol


class DateBehavior(MRJob):

    OUTPUT_PROTOCOL = TextProtocol

    def mapper(self, _, line):
        lineinfo = line.strip().split(',')
        if len(lineinfo) == 6:
            yield lineinfo[4], (lineinfo[3], 1)

    # def combiner(self, key, values):
    #
    #     dict = {'pv':  0, 'buy': 0, 'cart': 0, 'fav': 0}
    #
    #     for (behavior, count) in values:
    #         dict[behavior] = dict[behavior] + count
    #
    #     yield key, dict

    def reducer(self, key, values):
        dict = {'pv':  0, 'buy': 0, 'cart': 0, 'fav': 0}

        for (behavior, counts) in values:
            dict[behavior] = dict[behavior] + counts

        value_str = ''

        for num in dict.values():
            value_str += str(num) + '\t'

        yield key, value_str.strip()


if __name__ == '__main__':
    DateBehavior.run()
